//package com.dome.config;
//
//import com.dome.common.result.JsonResult;
//import com.dome.common.utils.RedisUtils;
//import com.dome.common.constant.BaseConstant;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import org.apache.commons.lang.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.web.filter.OncePerRequestFilter;
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.io.PrintWriter;
//import java.util.Objects;
//
///**
// * @author tang
// */
//@Component
//@WebFilter(filterName="token",urlPatterns= {"/*"})
//public class WebTokenFilter extends OncePerRequestFilter {
//
//    @Autowired
//    private RedisUtils redisUtils;
//
//    @Override
//    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//        String token = request.getHeader("token");
//        if (request.getRequestURI().indexOf("/loginToken") >= 0) {
//            filterChain.doFilter(request, response);
//        }else {
//            if(StringUtils.isBlank(token)){
//                JsonResult<Object> jsonResult = JsonResult.failCodeMessage("400", "token is null");
//                try {
//                    responseOutWithJson(response, jsonResult);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }else {
//                if(Objects.isNull(redisUtils.get(token))){
//                    JsonResult<Object> jsonResult = JsonResult.failCodeMessage("500", "非法token");
//                    try {
//                        responseOutWithJson(response, jsonResult);
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                }else {
//                    redisUtils.setExpire(token, BaseConstant.TOKEN_EXPIRE_TIME);
//                    filterChain.doFilter(request,response);
//                }
//            }
//        }
//    }
//
//    protected void responseOutWithJson(HttpServletResponse response, Object responseObject) throws Exception {
//        ObjectMapper mapper = new ObjectMapper();
//        String jsonStr = mapper.writeValueAsString(responseObject);
//        response.setCharacterEncoding("UTF-8");
//        response.setContentType("application/json; charset=utf-8");
//        PrintWriter out = null;
//        try {
//            out = response.getWriter();
//            out.append(jsonStr);
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            if (out != null) {
//                out.close();
//            }
//        }
//    }
//}
